package com.mes.web.controller.lc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.lc.MaterialReturn;
import com.mes.entity.lc.MaterialStock;
import com.mes.entity.tdd.ProcurementChuku;
import com.mes.entity.tdd.ProcurementChukuMateriel;
import com.mes.entity.tdd.ProcurementRuku;
import com.mes.service.lc.MaterialComeKuService;
import com.mes.service.lc.MaterialReturnService;
import com.mes.service.lc.MaterialStockService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 资材退货controller
 * @ClassName:MaterialReturnController
 * @Description:
 * @Author LC
 * @Date 2018年1月29日下午3:33:28
 * @version 1.0
 */
@Controller
@RequestMapping("/maretc")
public class MaterialReturnController {

	@Autowired
	MaterialReturnService	materialReturnService; //资材退货
	
	@Autowired
	MaterialComeKuService materialComeKuService; // 资材入库
	
	@Autowired
	MaterialStockService   materialStockService; //物料
	
	/**
	 * 多条件分页查询
	 * @param stockfy
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception
	 */
	@RequestMapping("/selectfymaret")
	public @ResponseBody void selectfymaret(
			MaterialReturn materialReturn,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			materialReturn.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			materialReturn.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(materialReturn.getMreodd())) {
				materialReturn.setMreodd(null);
			}//退货单号
			if (StringUtil.isEmpty(materialReturn.getMrepici())) {
				materialReturn.setMrepici(null);
			}//批次
	
			// 物料
			List<MaterialReturn> cslist = materialReturnService.selectfymaret(materialReturn);
			int a = materialReturnService.selectfymaretcont(materialReturn);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(cslist, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * PDA接口      查询所有未退货且审批通过的资材退货单
	 */
	@RequestMapping("/selectpckall")
	@ResponseBody
	public Map<String,Object> selectpckall(HttpServletResponse response, HttpServletRequest request) throws Exception{
		List<ProcurementChuku> list = materialReturnService.selectpckall();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("success", "1");
		map.put("data",list);
		return map;
	}
	
	
	/**
	 * PDA 接口，拿到一个收货单id，然后给出对应的具体信息
	 * 
	 */
	@RequestMapping("/selectpckdet")
	public void selectpckdet(Long ids, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		//根据收货单id查询对应的具体信息
		List<ProcurementChukuMateriel> list = materialReturnService.selectpckdet(ids);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data", list);
		ResponseUtil.write(response, jsonObject);
			
	}
	
	
	/**
	 * PDA  开始退货
	 * @param ids 退货单主键
	 * @param number 退货单编号
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/savepcm")
	public void savepcm(Long ids,String number,HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		
		//根据退货单id查询对应的具体信息
		List<ProcurementChukuMateriel> list = materialReturnService.selectpckdet(ids);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		String nowtime = dateFormat.format(now);
		//先判断该退货单是否已经入库
		if(list.size() > 0){
		
			//拿到资材入库主键
			String rukuid = list.get(0).getProcurementChuku().getPrukuid();
			Long ruid = Long.valueOf(rukuid);
			//根据入库主键查询是否入库
			ProcurementRuku pcru = materialComeKuService.selectallid(ruid);
			String isru = pcru.getPrstate();
			
			//退货之前先查询库存够不够退的(必须是入库过的物料)
			//根据物料编号查询物料库存
			List<MaterialStock>  materials = materialStockService.selectMaterialStock();
			//定义q 是否库存数量小于退货数量
			int q = 0;
			String qq = "该物料的库存数量不足退货：";
			//定义w 是否库存中没有对应物料
			int w = 0;
			String ww = "库存中没有该物料：";
			
			if("1".equals(isru)){
				
				for(int i = 0 ; i < list.size() ; i ++){
					//定义P  看是否库存中有该物料
					int p = 0;
					//出库物料编号
					String a = list.get(i).getMateriel().getNumber();
					for(int j = 0 ; j < materials.size() ; j ++){
						
						//如果编号相等
						if(a.equals(materials.get(j).getMateriel().getNumber())){
							//判断数量是否一样
							//退货数量
							Long pcmnum = Long.valueOf(list.get(i).getNumber());
							//库存数量
							Long mssnum = Long.valueOf(materials.get(j).getWnum());
							if(mssnum < pcmnum){
								q++;
								qq=qq+a+",";
							}
							p++;
						}
					}
					
					if(p == 0){
						w++;
						ww = ww + a+",";
					}
				}
			}

			if(w > 0){
				
				System.err.println(ww);
				
			}else if(q > 0){
				
				System.err.println(qq);
				
			} else{
				
				int a = 0;
				for(ProcurementChukuMateriel pcm : list){
					a = materialReturnService.savepcm(number,isru,nowtime,pcm);
				}
				
				//修改资材退货单状态
				if(a > 0){
					materialReturnService.updateProcurementChuku(ids);
				}
				
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("success", "1");
				jsonObject.put("data", a);
				ResponseUtil.write(response, jsonObject);
			}
		}
	}
	
}
